package com.up.vms.application.service;

import com.up.vms.application.common.DomainUtils;
import com.up.vms.domain.module.ShortVideoDTO;
import com.up.vms.domain.vm.entity.ShortVideo;
import com.up.vms.domain.vm.enums.ShortVideoStatus;
import com.up.vms.domain.vm.enums.VideoType;
import com.up.vms.domain.vm.service.ShortVideoService;
import com.up.vms.infrastructure.mvc.GeneRespDTO;
import com.up.vms.infrastructure.mvc.RespCode;
import com.up.vms.infrastructure.mvc.ReturnCode;
import com.up.vms.infrastructure.util.CommonUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

@Service
@Transactional
@Slf4j
public class ShortVideoApplicationService {
    @Autowired
    private ShortVideoService shortVideoService;

    public GeneRespDTO<HashMap> getShortVideoList(ShortVideoDTO shortVideoDTO) throws Exception {
        GeneRespDTO geneRespDTO = null;

        Page<ShortVideo> simpleShortVideos = null;
        simpleShortVideos = shortVideoService.findAllByCase(shortVideoDTO);
//        if (CommonUtils.isNotNull(shortVideoDTO.getStatus())) {
//        } else {
//            simpleShortVideos = shortVideoService.findAllByPageable(shortVideoDTO);
//        }

        if (CommonUtils.isNotNull(simpleShortVideos)) {
            geneRespDTO = DomainUtils.convert(simpleShortVideos);
        } else {
            throw new Exception("没有记录");
        }
        return geneRespDTO;
    }


    public GeneRespDTO saveShortVideo(ShortVideo shortVideo) throws Exception {
        GeneRespDTO geneRespDTO = new GeneRespDTO();
        ShortVideo saveShort = null;
        if (CommonUtils.isNotNull(shortVideo.getShortId())) {
            saveShort = shortVideoService.findShortVideoById(shortVideo.getShortId());
            saveShort.setShortName(shortVideo.getShortName());
            saveShort.setShortDesc(shortVideo.getShortDesc());
            saveShort.setShortUrl(shortVideo.getShortUrl());
            saveShort.setVideoUrl(shortVideo.getShortUrl());
            saveShort.setTelephone(shortVideo.getTelephone());
            saveShort.setUploader(shortVideo.getUploader());

            saveShort.setCategoryId(shortVideo.getCategoryId());
            saveShort.setCategory(shortVideo.getCategory());
            saveShort.setShowMode(shortVideo.getShowMode());
            saveShort.setShowVideoMode(shortVideo.getShowVideoMode());
            saveShort.setStatus(saveShort.getShowVideoMode());
            saveShort.setSorting(shortVideo.getSorting());
            saveShort.setRecommendFlag(shortVideo.getRecommendFlag());
            saveShort.setModified(new Date());
        } else {
            saveShort = shortVideo;
            saveShort.setPlayTimes(0);
            saveShort.setVideoUrl(shortVideo.getShortUrl());
            saveShort.setStatus(ShortVideoStatus.REVIEWING.code);
        }
        saveShort.setType(VideoType.SHORT.code);

        if (CommonUtils.isNotNull(shortVideo.getShowImage())) {
            saveShort.setShowImage(shortVideo.getShowImage());
        } else {
            if (CommonUtils.isNotNull(shortVideo.getShortUrl())) {
                saveShort.setShowImage(shortVideo.getShortUrl() + "?vframe/jpg/offset/1/w/678/h/380");
            }
        }

        ShortVideo newShort = shortVideoService.save(saveShort);

        geneRespDTO.setData(newShort);
        geneRespDTO.setRespCode(new RespCode(ReturnCode.SUCCESS));

        return geneRespDTO;
    }

    public GeneRespDTO delete(List<String> shortIdList) {
        GeneRespDTO<List> geneRespDTO = new GeneRespDTO();
        for (String idStr : shortIdList) {
            Long shortId = Long.parseLong(idStr);
            ShortVideo shortVideo = shortVideoService.findShortVideoById(shortId);
            if (shortVideo != null) {
                shortVideo.setStatus(9);
                shortVideoService.save(shortVideo);
            }
        }

        geneRespDTO.setRespCode(new RespCode(ReturnCode.SUCCESS));
        geneRespDTO.setData(shortIdList);
        return geneRespDTO;
    }

    public GeneRespDTO delete(Long shortId, String creator) {
        GeneRespDTO geneRespDTO = new GeneRespDTO();
        ShortVideo shortVideo = shortVideoService.findShortVideoByIdAndCreator(shortId, creator);
        if (shortVideo != null) {
            shortVideo.setStatus(9);
            shortVideoService.save(shortVideo);
        }

        geneRespDTO.setRespCode(new RespCode(ReturnCode.SUCCESS));
        geneRespDTO.setData(shortId);
        return geneRespDTO;
    }

    public GeneRespDTO review(List<String> shortIdList, Integer reviewResult) {
        GeneRespDTO geneRespDTO = new GeneRespDTO();
        for (String idStr : shortIdList) {
            Long shortId = Long.parseLong(idStr);
            ShortVideo shortVideo = shortVideoService.findShortVideoById(shortId);
            if (CommonUtils.isNotNull(shortVideo)) {
                if (shortVideo.getStatus() == ShortVideoStatus.REVIEWING.code) {
                    shortVideo.setStatus(reviewResult);
                    shortVideo.setModified(new Date());
                    shortVideoService.save(shortVideo);
                } else {
                    // 已审核/驳回，不处理
                }
            }
        }
        geneRespDTO.setRespCode(new RespCode(ReturnCode.SUCCESS));
        geneRespDTO.setData(shortIdList);

        return geneRespDTO;
    }


    public GeneRespDTO queryAppById(Long id) {
        GeneRespDTO geneRespDTO = new GeneRespDTO();

        ShortVideo video = shortVideoService.findShortVideoById(id);
        shortVideoService.updatePlayTimes(id);
        if (video != null) {
            geneRespDTO.setRespCode(new RespCode(ReturnCode.SUCCESS));
            geneRespDTO.setData(video);
        } else {
            geneRespDTO.setRespCode(new RespCode(ReturnCode.SERVICE_ERROR));
        }
        return geneRespDTO;
    }

    public GeneRespDTO queryById(Long id) {
        GeneRespDTO geneRespDTO = new GeneRespDTO();

        ShortVideo video = shortVideoService.findShortVideoById(id);
        if (video != null) {
            geneRespDTO.setRespCode(new RespCode(ReturnCode.SUCCESS));
            geneRespDTO.setData(video);
        } else {
            geneRespDTO.setRespCode(new RespCode(ReturnCode.SERVICE_ERROR));
        }
        return geneRespDTO;
    }
}
